﻿// 选项Model
Ext.define('Maintenance', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
	    , "Category"   //分类
	    , "Code"   //编号
	    , "Name"   //名称
	    , "Order"   //序号       
    ],
    idProperty: "Id"
});

// 选项下拉框
Ext.define("SHApp.System.Maintenance.MaintenComBox", {
    extend: "BLBTSystem.CmbData",
    alias: "widget.maintenComboBox",
    dataUrl: "api/maintenanceQ?format=json",
    model: "Maintenance",
    matchFieldWidth: false,
    matchClass: "所有分类",
    hasAll: false,
    sorters: [{
        property: 'Order',
        direction: 'ASC'
    }],
    listConfig: {
        resizable: true,
        width: 300
    },
    minChars: 1,
    valueField: "Name",
    displayField: "Name",
    initComponent: function () {
        Ext.apply(this);
        this.callParent(arguments);
    },
    loadData: function () {
        var me = this;
        var myUrl = me.dataUrl + "&Category=" + encodeURIComponent(this.matchClass);
        var itemStore = Ext.create("Ext.data.Store", {
            model: me.model,
            sorters: me.sorters,
            proxy: {
                type: 'ajax',
                url: myUrl,
                reader: {
                    type: 'json',
                    root: 'Data',
                    totalProperty: 'TotalCount'
                }
            }

        });
        itemStore.on("load", function (sender) {
            if (me.allmodel) {
                sender.insert(0, me.allmodel);
            }
        });
        itemStore.load({
            callback: function (records, options, success) {

                if (success) {
                    if (records.length == 0) {
                        return;
                    }
                    var s = me.selectIndex;
                    if (me.selectIndex < 0) {
                        return;
                    }
                    if (me.selectIndex > records.length) {
                        return;
                    }
                    //  Ext.Array.insert(records, 0, Ext.create(me.model));                   
                    me.select(records[s]);
                    var arr = new Array();
                    arr.push(records[s]);
                    me.fireEvent("select", me, arr);

                    if (me.hasAll) {
                        itemStore.add({ Code: "00", Name: "全部" });
                    }
                }
            }
        });
        this.store = itemStore;
    },
    loadByCategory: function (cate) {
        this.dataUrl = this.dataUrl + "&Category=" + encodeURIComponent(cate);
        this.reLoadData();
    }
});
